const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList:[],
    images: [],
    indicatorDots: false,
    vertical: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    usernavId:0,
    list:[],
    intPageSize:15,
    intCurrPage:1,
    type:'',
    key:'',
    noMore:false,
    searchflag:false,
    TcLev:1,
    usernavIdNav2:'',
    news_box_height:'',
    PrevideoID:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var url = "api/FeijiuApp/GetToutiaoHd?strRSA=";
    app.getDaoGet(url,function (r) {   
    
      if (r.code == 200) {
        that.setData({
          images:r.data
        })
      }
    })
    wx.getSystemInfo({
      success:function (res) {
        that.setData({
          news_box_height:(res.windowHeight-45)+'px'
        })
        wx.setStorageSync('windowHeight',res.windowHeight);
      }
    })
    this.getNewsList();
    this.Post_FJ_GetToutiaoClass();
    wx.showShareMenu({
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  toInfo:function (e) {
    wx.navigateTo({
      url: 'newsinfo?tid='+e.currentTarget.dataset.tid + '&type=' + e.currentTarget.dataset.type
    })  
  },
  Post_FJ_GetToutiaoClass:function () {
    var that = this;
    app.getDaoPost('api/FeijiuApp/Post_FJ_GetToutiaoClass',{},function (r) {
      
      if (r.code == 200) {
        for (var i = 0; i < r.data.length; i++) {
          r.data[i].type = 'c'
        }
      }
      var arr = [{TcName:'首页',TcLev:1,type:0,TcID:0,list:[]},{TcName:'精选',type:'t',TcID:1,TcLev:1,list:[]},{TcName:'推荐',type:'t',TcID:2,TcLev:1,list:[]}];
      var list1 = arr.concat(r.data);
      that.setData({
        navList:list1
      })
    })
  },  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  toList:function (e) {
   
    if (e.currentTarget.dataset.typee != this.data.type || e.currentTarget.dataset.keyy != this.data.key) {
      if (e.currentTarget.dataset.keyy == 20) {
        wx.navigateTo({
          url: '../wenda/index'
        })
        return false;
      }
        this.setData({
          type:e.currentTarget.dataset.typee,
          key:e.currentTarget.dataset.keyy,
          usernavId:e.currentTarget.dataset.index,
          noMore:false,
          list:[],
          intCurrPage:1,
          usernavIdNav2:''
        }) 
        var windowHeight = wx.getStorageSync('windowHeight');   
        if (this.data.navList[e.currentTarget.dataset.index].list.length > 0) {
          this.setData({
            news_box_height:(windowHeight-54-45)+'px'
          })          
        }else{
          this.setData({
            news_box_height:(windowHeight-45)+'px'
          })          
        }
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 10
      })  
      this.getNewsList();
    }
  },
  toList2:function (e) {
    if (e.currentTarget.dataset.typee != this.data.type || e.currentTarget.dataset.keyy != this.data.key) {
        this.setData({
          type:e.currentTarget.dataset.typee,
          key:e.currentTarget.dataset.keyy,
          usernavIdNav2:e.currentTarget.dataset.index,
          noMore:false,
          list:[],
          intCurrPage:1
        }) 
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 10
      })  
      this.getNewsList();
    
    }

  },
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.Post_FJ_GetToutiaoList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  AddTtZan:function (e) {
    var that = this;
    console.log(e)
    let list = that.data.list
    
    var api_input = {
       tid:e.currentTarget.dataset.tid,
       strRSA:app.data.strRSA
    }
    if(list[e.currentTarget.dataset.index].isZan == true){
      wx.showToast({
        title: '您已经点过赞了',
        icon:'none'
      })
      return;
    }
   app.getDaoPostOld('api/FeijiuApp/AddTtZan',api_input,function (r) {      
     if (r.code == 200) {
       app.tishi('点赞成功!');
      list[e.currentTarget.dataset.index].Zan = list[e.currentTarget.dataset.index].Zan+1,
      list[e.currentTarget.dataset.index].isZan = true
     }
     that.setData({
       list:list
     })
   }) 
 },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list:[],
      intCurrPage:1
    })
    this.getNewsList();
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom1: function () {
    if (!this.data.noMore) {
      this.getNewsList();
    }
  },

  /**
   * 用户点击右上角分享
   */

  onShareAppMessage: function (e) {
    console.log(e)
  　　var that = this;  　　
  　　var shareObj = {
  　　　　title: e.target.dataset.title,      
  　　　　path: '/pages/news/newsinfo?tid=' + e.target.dataset.tid + "&type=" + 15,  
  　　　　imgUrl: '',
  　　}　
  　　return shareObj; 
  },

  getNewsList:function () {
    var that = this;
    var api_input = {
      type:that.data.type,
      Key:that.data.key,
      strCity:'',
      strProvice:'',
      intPageSize:that.data.intPageSize,
      intCurrPage:that.data.intCurrPage
    }     

    app.getDaoPost('api/FeijiuApp/Post_FJ_GetToutiaoList',api_input,function (r) {
   
      if (r.code == 200) {
        if (r.data.length > 0) {
          for (var i = 0; i < r.data.length; i++) {
            r.data[i].addtime = r.data[i].addtime.replace('T'," ").substring(5,16);

            if (r.data[i].photo) {
              r.data[i].photo = app.photoSplitOne(r.data[i].photo)
            }
          }
          var list1 = that.data.list.concat(r.data);
          that.setData({
            list:list1,
            intCurrPage:that.data.intCurrPage+1
          })          
        }else{
          that.setData({
            noMore:true
          })
        }       
      }
    })    
    // var that = this;
    // wx.showToast({
    //   title: "加载中",
    //   icon: "loading",      
    // })    
    // app.getDaoGet("api/FeijiuApp/GetToutiaoList?type="+that.data.type+"&Key="+that.data.key+"&strCity=&strProvice=&intPageSize="+that.data.intPageSize+"&intCurrPage="+that.data.intCurrPage+"&strRSA=",function (r) {
    //   console.log(r)
    //   if (r.code == 200) {
    //     if (r.data.length > 0) {
    //       for (var i = 0; i < r.data.length; i++) {
    //         r.data[i].addtime = r.data[i].addtime.replace('T'," ");
    //         if (r.data[i].photo) {
    //           r.data[i].photo = app.photoSplitOne(r.data[i].photo)
    //         }
    //       }
    //       var list1 = that.data.list.concat(r.data);
    //       that.setData({
    //         list:list1,
    //         intCurrPage:that.data.intCurrPage+1
    //       })          
    //     }else{
    //       that.setData({
    //         noMore:true
    //       })
    //     }
    //   }
    //   wx.hideToast()
    // }) 
  },
  Post_FJ_GetToutiaoList:function () {
    var that = this;
    var api_input = {
      type:that.data.type,
      Key:that.data.key,
      strCity:'',
      strProvice:'',
      intPageSize:that.data.intPageSize,
      intCurrPage:that.data.intCurrPage
    } 
     
    app.getDaoPost('api/FeijiuApp/Post_FJ_GetToutiaoList',api_input,function (r) {
     
      if (r.code == 200) {
        if (r.data.length > 0) {
          for (var i = 0; i < r.data.length; i++) {
            r.data[i].addtime = r.data[i].addtime.replace('T'," ");
            if (r.data[i].photo) {
              r.data[i].photo = app.photoSplitOne(r.data[i].photo)
            }
          }
          var list1 = that.data.list.concat(r.data);
          that.setData({
            list:list1,
            intCurrPage:that.data.intCurrPage+1
          })          
        }else{
          that.setData({
            noMore:true
          })
        }       
      }
    })
  },
  // 视频播放
  handlePlay(event){
    let that = this
    //当前即将要播放的视频ID
    let videoID = event.currentTarget.id
    console.log(videoID)
    //判断是否正在有视频播放
    if(that.data.PrevideoID){
      //有正在播放的视频
      //判断和上一个视频是否是同一个视频
      if(that.data.PrevideoID != videoID){
        //不是同一个视频就暂停上一个视频播放
        wx.createVideoContext(that.data.PrevideoID).stop()
        //播放当前视频
        wx.createVideoContext(videoID).play()
        //更新prevideoID
        that.setData({
          PrevideoID:videoID
        })
      }
    }
    //没有正在播放的视频就直接保存videoID
    else{
      that.setData({
        PrevideoID:videoID
      })
    }
  },

})